/*
 * Licensed to Nanjing RedOrange Co. ltd (RedOrange) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * RedOrange licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */
package redora.impl;
import java.util.HashSet;
import java.util.Set;

import redora.api.Persistable;
import redora.api.PersistableNtoMSet;

public class PersistableNtoMHashSet<E> extends PersistableHashSet<E>
     implements PersistableNtoMSet<E> {

    private static final long serialVersionUID = 1L;
    /** List of recently added relations with already existing.
     * counterparts (isNew = false, id != null)
     */
    private final Set<Long> addedObjects = new HashSet<Long>();

    public Set<Long> getAddedObjects() {
        return addedObjects;
    }

    /**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * default initial capacity (16) and load factor (0.75).
     * <br />Unchanged from HashMap implementation
     */
    public PersistableNtoMHashSet() {
        super();
    }

    /**
     * Adds the specified element to this set if it is not already present.
     * More formally, adds the specified element <tt>e</tt> to this set if
     * this set contains no element <tt>e2</tt> such that
     * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
     * If this set already contains the element, the call leaves the set
     * unchanged and returns <tt>false</tt>.
     *
     * @param e element to be added to this set
     * @return <tt>true</tt> if this set did not already contain the specified
     * element
     */
    public boolean add(E e) {
        if (super.add(e)) {
            if (((Persistable) e).getId() != null) {
                addedObjects.add(((Persistable) e).getId());
            }
            return true;
        }
        return false;
    }

    /**
     * Removes the specified element from this set if it is present.
     * More formally, removes an element <tt>e</tt> such that
     * <tt>(o==null ? e==null : o.equals(e))</tt>,
     * if this set contains such an element.  Returns <tt>true</tt> if
     * this set contained the element (or equivalently, if this set
     * changed as a result of the call).  (This set will not contain the
     * element once the call returns.)
     *
     * @param o object to be removed from this set, if present
     * @return <tt>true</tt> if the set contained the specified element
     */
    @Override
    public boolean remove(Object o) {
        if (super.remove(o)) {
            if (((Persistable) o).getId() != null) {
                addedObjects.remove(((Persistable) o).getId());
            }
            return true;
        }
        return false;
    }

    /**
     * Removes all of the elements from this set.
     * The set will be empty after this call returns.
     */
    @Override
    public void clear() {
        for (Long i : map.keySet()) {
            if (i > 0) {
                addedObjects.remove(i);
            }
        }
        super.clear();
    }

    public boolean removeById(Long id) {
        addedObjects.remove(id);
        return super.removeById(id);
    }

    @Override
    public boolean isDirty() {
        if (super.isDirty()) {
            return true;
        }
        if (!getAddedObjects().isEmpty()) {
            return true;
        }

        return false;
    }
}
